<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" v-show="showSearch"  label-width="auto"
      label-position="left">
      <el-row>



        <el-col :span="6">
          <el-form-item label="姓名" prop="custName">
            <el-input v-model="queryParams.custName" placeholder="请输入客户姓名" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="手机号" prop="mobile">
            <el-input v-model="queryParams.mobile" placeholder="请输入客户手机号" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="ID" prop="id">
            <el-input v-model="queryParams.id" placeholder="请输入客户ID" clearable @keyup.enter.native="handleQuery" />
          </el-form-item>
        </el-col>


        <el-col :span="6">
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-col>
      </el-row>

    </el-form>



    <el-table v-loading="loading" :data="custinfoList" @selection-change="handleSelectionChange" fit style="width: 100%">
      <el-table-column type="selection" align="center" min-width="3%"></el-table-column>
      <el-table-column label="序号" type="index" align="center" min-width="3%">
        <template slot-scope="scope">
          <span>{{ (queryParams.pageNum - 1) * queryParams.pageSize + scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="ID" align="center" prop="id" min-width="3%" />
      
      <el-table-column label="姓名" align="center" prop="custName" min-width="3%" >
        <template slot-scope="scope">
          {{ encryptionName(scope.row.custName) }}
        </template>
      </el-table-column>

      <el-table-column label="公共池客户" align="center" prop="inPublic" min-width="3%">
        <template slot-scope="scope">
          <span v-show="scope.row.empId == null && scope.row.orgId != null">是</span>
          <span v-show="scope.row.empId != null || scope.row.orgId == null">否</span>
        </template>
      </el-table-column>


      <el-table-column label="离职人员客户" align="center" prop="inWithdrawn" min-width="3%">
        <template slot-scope="scope">
          <span v-show="scope.row.empId != null && scope.row.isDelete == '1'">是</span>
          <span v-show="scope.row.empId == null || scope.row.isDelete != '1'">否</span>
        </template>
      </el-table-column>
      <el-table-column label="年龄" align="center" prop="age" min-width="3%" />
      <el-table-column label="状态" align="center" prop="status" min-width="3%">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.crm_customerStatus" :value="scope.row.status" min-width="3%" />
        </template>
      </el-table-column>
      <el-table-column label="备注搜索" align="left" width="280" prop="remark" min-width="3%">
        <template slot-scope="scope">
          <span v-html='scope.row.remark' class="span_remark"></span>
        </template>
      </el-table-column>
      <el-table-column label="部门归属" align="center" prop="orgId" min-width="3%">
        <template slot-scope="scope">
          {{ getDeptByOrgId(scope.row.deptId) }}
        </template>
      </el-table-column>

      <el-table-column label="顾问" align="center" prop="empId" min-width="3%">
        <template slot-scope="scope">
          {{ getNameByEmpId(scope.row.userId) }}
        </template>
      </el-table-column>
      <el-table-column label="城市" align="center" prop="city" min-width="3%" />
      <el-table-column label="星级" align="center" prop="starLevel" min-width="3%">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.crm_star" :value="scope.row.starLevel" />
        </template>
      </el-table-column>
      <el-table-column label="申请额度" align="center" prop="amountType" min-width="3%">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.crm_amount" :value="scope.row.amountType" />
        </template>
      </el-table-column>
      <el-table-column label="最近处理" align="center" prop="updateTime" min-width="3%"><template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="申请时间" align="center" prop="applyTime" min-width="3%"><template slot-scope="scope">
          <span>{{ parseTime(scope.row.applyTime) }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
      @pagination="getList" />

  </div>
</template>

<script>
import {
  listCustinfo,
  delCustinfo,
  allCustlist
} from "@/api/crmsys/custinfo/custinfo";

import { childrenAlldeptTree } from "@/api/system/user";

import Treeselect from "@riophae/vue-treeselect";
import "@riophae/vue-treeselect/dist/vue-treeselect.css";

export default {
  name: "AllCust",
  dicts: [
    "crm_yesno",
    "crm_sex",
    "crm_has",
    "crm_star",
    "crm_type",
    "crm_paytype",
    "crm_customerStatus",
    "crm_marriage",
    "crm_work",
    "crm_roleCategory",
    "crm_amount"
  ],
  components: { Treeselect },
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 客户信息表格数据
      custinfoList: [],
      // 弹出层标题
      title: "",
      //是否
      whether: [{ label: "是", value: "1" }, { label: "否", value: "0" }],
      // 是否显示弹出层
      open: false,
      applyTime: null,
      updateTime: null,
      oldDistributeTime: null,
      reDistributeTime: null,
      lastRemarkTime: null,
      lastIntroduceTime: null,
      lastVisitTime: null,
      lastSignTime: null,
      lastLoanTime: null,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        id: null,
        custName: null,
        mobile: null,
        age: null,
        sex: null,
        household: null,
        officeTel: null,
        marriaged: null,
        hasHouse: null,
        houseType: null,
        hasCar: null,
        hasCredit: null,
        hasPolicy: null,
        hasSocial: null,
        socialYears: null,
        socialAmt: null,
        hasFund: null,
        fundYears: null,
        fundAmt: null,
        workNature: null,
        workPaytype: null,
        hasCompany: null,
        debtAmt: null,
        debtOverdue: null,
        loverKnowed: null,
        zhimaScore: null,
        wedebtLimit: null,
        orgId: null,
        empId: null,
        starLevel: null,
        status: null,
        remark: null,
        localPersonFlag: null,
        localPhoneFlag: null,
        amountType: null,
        hasLocalHouse: null,
        custtext: null,
        custtype: null,
        followTimeStart: null,
        followTimeEnd: null,
        loan_content: null,
        loan_condition: null,
        unfollow_day: null,
        ageLimit: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
      pickerOptions: {
        shortcuts: [{
          text: '最近一周',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近一个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
            picker.$emit('pick', [start, end]);
          }
        }, {
          text: '最近三个月',
          onClick(picker) {
            const end = new Date();
            const start = new Date();
            start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
            picker.$emit('pick', [start, end]);
          }
        }]
      },
      value1: [new Date(2000, 10, 10, 10, 10), new Date(2000, 10, 11, 10, 10)],
      value2: '',
      // 部门归属树选项
      deptOptions: [undefined],
    };
  },
  created() {
    // 页面跳转重置搜索条件
    if (this.$route.query.queryParams != null && this.$route.query.queryParams != undefined && typeof this.$route.query.queryParams === 'object') {
      this.queryParams = this.$route.query.queryParams;
    }
    this.getList();
    this.getDeptTree();
  },
  activated() {
    const time = this.$route.query.t;
    if (time != null && time != this.uniqueId) {
      this.uniqueId = time;
      this.queryParams.pageNum = Number(this.$route.query.pageNum);
      this.getList();
    }
  },
  methods: {
    /** 查询客户信息列表 */
    getList() {
      this.loading = true;
      allCustlist(this.queryParams).then(response => {
        console.log(response);
        this.custinfoList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        custName: null,
        mobile: null,
        age: null,
        sex: null,
        city: null,
        household: null,
        officeTel: null,
        marriaged: null,
        hasHouse: null,
        houseType: null,
        houseArea: null,
        houseValue: null,
        houseTimes: null,
        houseMonthPay: null,
        houseBankName: null,
        hasCar: null,
        carType: null,
        carTimeLimit: null,
        carValue: null,
        carTimes: null,
        carMonthPay: null,
        carBankName: null,
        hasCredit: null,
        creditAmount: null,
        creditOverdue: null,
        hasPolicy: null,
        policyCompany: null,
        policyPayType: null,
        policyPayTime: null,
        hasSocial: null,
        socialYears: null,
        socialAmt: null,
        hasFund: null,
        fundYears: null,
        fundAmt: null,
        hasWork: null,
        workNature: null,
        workSalary: null,
        workPaytype: null,
        hasCompany: null,
        hasTax: null,
        debtAmt: null,
        debtOverdue: null,
        loverKnowed: null,
        zhimaScore: null,
        wedebtLimit: null,
        applyLimit: null,
        applyTime: null,
        updateTime: null,
        distributeTime: null,
        oldDistributeTime: null,
        reDistributeTime: null,
        reDistributeStatus: null,
        mediaSource: null,
        channelSource: null,
        orgId: null,
        empId: null,
        starLevel: null,
        status: null,
        preAuditLimit: null,
        preAuditChannel: null,
        remark: null,
        dpRemark: null,
        wishRemark: null,
        distributeRemark: null,
        hasRemind: null,
        lastUpdateBy: null,
        localPersonFlag: null,
        localPhoneFlag: null,
        candoFlag: null,
        oldEmpId: null,
        qrcodeid: null,
        isDelete: null,
        urlTrack: null,
        channelDesc: null,
        channelPsource: null,
        education: null,
        amountType: null,
        lastRemarkTime: null,
        repeatFlag: null,
        lockFlag: null,
        lockTime: null,
        hasLocalHouse: null,
        distributeId: null,
        passflag: null,
        md5val: null,
        encval: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");

      this.applyTime = null;
      this.updateTime = null;
      this.oldDistributeTime = null;
      this.reDistributeTime = null;
      this.lastRemarkTime = null;
      this.lastIntroduceTime = null;
      this.lastVisitTime = null;
      this.lastSignTime = null;
      this.lastLoanTime = null;

      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加客户信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      const custName = row.custName;
      const params = { id: id, queryParams: this.queryParams, pageNum: this.queryParams.pageNum, form: this.form, routePath: this.$route.path };
      this.$tab.openPage(
        "修改客户信息",
        "/crmsys/custedit/info/",
        params
      );
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal
        .confirm('是否确认删除客户信息编号为"' + ids + '"的数据项？')
        .then(function () {
          return delCustinfo(ids);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "crmsys/custinfo/export",
        {
          ...this.queryParams
        },
        `custinfo_${new Date().getTime()}.xlsx`
      );
    },
    /** 查询部门归属下拉树结构 */
    getDeptTree() {

      var dept = { deptId: this.$store.state.user.deptid }
      childrenAlldeptTree(dept).then(response => {
        this.deptOptions = response.data;
      });
    }
  }
};
</script>
<style lang="scss" scoped>
@import '@/assets/styles/command.scss';
</style>